package servlet;

import entity.Person;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import javax.persistence.PersistenceUnit;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;

/**
 * @author T00b!b
 */
@WebServlet(name = "UpdatePersonServlet", urlPatterns = {"/UpdatePerson"})
public class UpdatePersonServlet extends HttpServlet {

    @PersistenceUnit
    //The emf corresponding to 
    private EntityManagerFactory emf;  
    
    @Resource
    private UserTransaction utx;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //Find person by ID
        Person person =  emf.createEntityManager().find(Person.class, Long.valueOf(request.getParameter("id")));
        //Set request atrribute
        request.setAttribute("person", person);
        //Load jsp
        request.getRequestDispatcher("UpdatePerson.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        assert emf != null;  //Make sure injection went through correctly.
        EntityManager em = null;
        try {
            //Get the data from person's form
            Long id = Long.valueOf(request.getParameter("id"));
            String login = (String) request.getParameter("login");
            String password = (String) request.getParameter("password");

            //begin a transaction
            utx.begin();
            //create an em. 
            //Since the em is created inside a transaction, it is associsated with 
            //the transaction
            em = emf.createEntityManager();
            Person person = em.find(Person.class, id);
            person.setLogin(login);
            person.setPassword(password);
            //persist the person entity
            em.persist(person);
            //commit transaction which will trigger the em to 
            //commit newly updated entity into database
            utx.commit();

            request.setAttribute("person", person);

            //Forward to ListPerson servlet to list persons along with the newly
            //updated person above
            request.getRequestDispatcher("ListPerson").forward(request, response);
        } catch (Exception ex) {
            throw new ServletException(ex);
        } finally {
            //close the em to release any resources held up by the persistence provider
            if(em != null) {
                em.close();
            }
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
